package com.lm.paymentadmin.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.*;
import java.util.*;

/**
 * @author liu2dog
 */
public class IkUtil {
    /**
     * ik切片后根据所在字典返回一个LinkedHashMap
     * @param msg
     * @return
     * @throws IOException
     */
    public static Map<String,String> cut(String msg,boolean userSmart) throws IOException {
        Map<String,String> map = new LinkedHashMap<String,String>();
        map.put(msg,"text");
        List<String> cityList = dicToArray(ClassLoader.getSystemResource("ik/city.dic").getPath());
        List<String> localList = dicToArray(ClassLoader.getSystemResource("ik/local.dic").getPath());
        StringReader sr=new StringReader(msg);
        IKSegmenter ik=new IKSegmenter(sr, userSmart);
        Lexeme lex=null;
        //词性分词
        while((lex=ik.next())!=null){
            if(cityList.contains(lex.getLexemeText())){
                map.put(lex.getLexemeText(),"city");
            }else if(localList.contains(lex.getLexemeText())){
                map.put(lex.getLexemeText(),"local");
            }
        }

        return map;
    }

    public static List<String> dicToArray(String filePath) throws IOException{
        List<String> list = new ArrayList<String>();
        BufferedReader bf= new BufferedReader(new FileReader(filePath));
        String s;        //定义并且赋初值
        while((s = bf.readLine())!=null)
        {
            //使用readLine方法，一次读一行
            //去掉s左右的空格并添加到buffer中
            list.add(s.trim());
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
        String text="牛头山一合肥";
        Map<String,String> map = IkUtil.cut(text,false);
        System.out.println(map.toString());

//        StringReader sr=new StringReader(text);
//        IKSegmenter ik=new IKSegmenter(sr, false);
//        Lexeme lex=null;
//        //词性分词
//        while((lex=ik.next())!=null){
//            System.out.println(lex.getLexemeText());
//        }
    }
}
